package logic

import (
	"context"

	"group/internal/svc"
	"group/pb/group"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetGroupListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetGroupListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetGroupListLogic {
	return &GetGroupListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// GetGroupList 群聊列表
func (l *GetGroupListLogic) GetGroupList(in *group.GroupListRequest) (*group.GroupListResponse, error) {
	total, err := l.svcCtx.ChatGroupAggregationModel.GetGroupCount(l.ctx, in.UserId)
	if err != nil {
		return nil, err
	}
	list, err := l.svcCtx.ChatGroupAggregationModel.GetGroupList(l.ctx, in.UserId, in.Offset, in.Limit)
	if err != nil {
		return nil, err
	}

	var data []*group.GroupInfo
	for _, item := range list {
		data = append(data, &group.GroupInfo{
			Id:       item.Id,
			UserId:   item.UserId,
			Name:     item.Name,
			Avatar:   item.Avatar,
			Desc:     item.Desc,
			Status:   item.Status,
			JoinType: item.JoinType,
			CreateAt: item.CreatedAt.Unix(),
			UpdateAt: item.UpdatedAt.Unix(),
		})
	}
	return &group.GroupListResponse{Total: total, Data: data}, nil
}
